const { letDataToJsFile } = require("./_utils");

const defaultOptions = {
  description: "新增/编辑",
  width: "600px",
  // attrs: {
  //   width: "600px",
  // },
  idKey: "id",
  apiPath: "@/api-mock",
  addApi: "PostMockCommonAdd",
  editApi: "PostMockCommonUpdate",
  // defaultValue: undefined,
};
module.exports = {
  /**
   *
   * @param {object[]} fields 要处理的表单字段（数组）
   * @param {object} options 配置信息
   * @param {string} description 页面描述
   * @returns
   */
  getAddEditStr(fields = [], options) {
    const { width, addApi, editApi, apiPath, defaultValue, idKey, description } = { ...defaultOptions, ...options };
    const defValsStr = defaultValue && Object.keys(defaultValue).length ? JSON.stringify(defaultValue) : "";
    return `<!-- ${description} -->
<template>
  <BaseForm
    style="width: ${width}"
    v-model="modelData"
    :fields="fields"
    :fetch="data ? ${editApi} : ${addApi}"
    :afterSuccess="() => refreshList()"
    :extraParams="data ? { ${idKey}: data.${idKey} } : undefined"
  >
  </BaseForm>
</template>
<script lang="ts" setup>
import { FormField } from "@/core/components/form/_types";
import { ${addApi}, ${editApi} } from "${apiPath}";
import { CommonObj, FinallyNext, StrNum } from "@/core/_types";

const props = withDefaults(
  defineProps<{
    data?: CommonObj;
    refreshList?: FinallyNext;
  }>(),
  {}
);
const modelData = reactive<CommonObj>({ ${defValsStr ? `...${defValsStr}, ` : ""}...props.data });
const fields: FormField[] = ${letDataToJsFile(fields)};
</script>
<style lang="scss" scoped></style>`;
  },
};
